package cn.yunlang.zhangZhongFei.base.pay.wxpay.util;

import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.List;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

/** 微信授权登陆工具类 */
public class WxLoginUtil {
  // 连接超时时间
  private static final int CONNECT_TIMEOUT = 10000;
  // 读取超时时间
  private static final int READ_TIMEOUT = 10000;

  public static JSONObject getUserInFor(String URL) {
    JSONObject jsonObject = null;
    HttpURLConnection connection = null;
    InputStream is = null;
    BufferedReader br = null;

    StringBuilder result = new StringBuilder();

    try {
      // 创建URL连接对象
      URL url = new URL(URL);
      // 通过创建的utl打开一个连接，强转成HttpURLConnection
      connection = (HttpURLConnection) url.openConnection();
      connection.setDoOutput(true);
      connection.setDoInput(true);
      // 创建超时时间和读取时间
      connection.setConnectTimeout(15000);

      connection.setReadTimeout(60000);
      connection.setRequestProperty("Accept", "application/json");
      // 发送请求
      connection.connect();

      // 通过connection获取输出流，并使用Reader读取
      if (200 == connection.getResponseCode()) {
        is = connection.getInputStream();
        br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        String line;
        while ((line = br.readLine()) != null) {
          result.append(line);
          //                        System.out.println(line);
        }
      } else {
        System.out.println("ResponseCode is an error code:" + connection.getResponseCode());
      }

    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (br != null) {
          br.close();
        }
        if (is != null) {
          is.close();
        }
      } catch (IOException io) {
        io.printStackTrace();
      }
      connection.disconnect();
    }
    jsonObject = JSONObject.parseObject(result.toString());
    return jsonObject;
  }

  public static String sendRestfulRequestStr(String url, String param, int connectTm, int readTm) {
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    int iConnectTm = connectTm;
    int iReadTm = readTm;
    if (iConnectTm < 1000) {
      iConnectTm = CONNECT_TIMEOUT;
    }
    if (iReadTm < 1000) {
      iReadTm = READ_TIMEOUT;
    }
    requestFactory.setConnectTimeout(iConnectTm);
    requestFactory.setReadTimeout(iReadTm);
    RestTemplate restTemplate = new RestTemplate(requestFactory);
    // 解决返回String中文乱码问题
    List<HttpMessageConverter<?>> list = restTemplate.getMessageConverters();
    for (HttpMessageConverter<?> httpMessageConverter : list) {
      if (httpMessageConverter instanceof StringHttpMessageConverter) {
        ((StringHttpMessageConverter) httpMessageConverter)
            .setDefaultCharset(Charset.forName("utf-8"));

        break;
      }
    }
    ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, param, String.class);
    return responseEntity.getBody();
  }
}
